﻿Imports EstoqueDAL
Public Class PECABLL
    Dim objDAL As PecaDAL

    'falta verificar o método valida replica na exclusão.

#Region "Propriedades"
    Property OrdCodigo As Integer
    Property VenCodigo As Integer
    Property pec_codigo As Integer
    Property pro_codigo As Integer
    Property clf_codigo As Integer
    Property fab_codigo As Integer
    Property sta_codigo As Integer
    Property cat_codigo As Integer
    Property pec_codbarra As String
    Property pec_qtdemin As Integer
    Property pec_qtdemax As Integer
    Property pec_qtde As Integer
    Property pec_ipi As Decimal
    Property pec_subtributaria As Decimal
    Property pec_original As String
    Property pec_valorunitario As Decimal
    Property pec_mcontrib As Decimal
    Property pec_margem As Decimal
    Property pec_abreviacao As String
    Property pec_codigofabricante As String
    Property pro_descricao As String
    Property pro_precovenda As Decimal
    Property pro_precocusto As Decimal
    Property pro_desconto As Decimal
#End Region

#Region "Construtores"
    Public Sub New()

    End Sub

    Public Sub New(ByVal codigo As Integer)
        carrega(codigo)
    End Sub
    Public Sub New(ByVal pec_codigo As Integer, ByVal pro_codigo As Integer, ByVal clf_codigo As Integer, ByVal fab_codigo As Integer, ByVal sta_codigo As Integer,
                            ByVal cat_codigo As Integer, ByVal pec_codbara As String, ByVal pec_qtdemin As Integer, ByVal pec_qtdemax As Integer, ByVal pec_qtde As Integer,
                            ByVal pec_ipi As Decimal, ByVal pec_subtributaria As Decimal, ByVal pec_original As String, ByVal pec_valorunitario As Decimal,
                            ByVal pec_mcontrib As Decimal, ByVal pec_margem As Decimal, ByVal pec_abreviacao As String, ByVal fab_codigofabricante As String,
                            ByVal pro_descricao As String, ByVal pro_precovenda As Decimal, ByVal pro_precocusto As Decimal, ByVal pro_desconto As Decimal)
        Me.pec_codigo = pec_codigo
        Me.pro_codigo = pro_codigo
        Me.clf_codigo = clf_codigo
        Me.fab_codigo = fab_codigo
        Me.sta_codigo = sta_codigo
        Me.cat_codigo = cat_codigo
        Me.pec_codbarra = pec_codbara
        Me.pec_qtdemin = pec_qtdemin
        Me.pec_qtdemax = pec_qtdemax
        Me.pec_qtde = pec_qtde
        Me.pec_ipi = pec_ipi
        Me.pec_subtributaria = pec_subtributaria
        Me.pec_original = pec_original
        Me.pec_valorunitario = pec_valorunitario
        Me.pec_mcontrib = pec_mcontrib
        Me.pec_margem = pec_margem
        Me.pec_abreviacao = pec_abreviacao
        Me.pec_codigofabricante = fab_codigofabricante
        Me.pro_descricao = pro_descricao
        Me.pro_precovenda = pro_precovenda
        Me.pro_precocusto = pro_precocusto
        Me.pro_desconto = pro_desconto
    End Sub
#End Region

#Region "Funções"
#Region "Inserir/alterar"
    Function inseriralterar(usuario As Integer) As Boolean
        Try
            If (Me.valida) Then
                If (validareplica().Rows.Count = 0) Then
                    objDAL = New PecaDAL
                    Me.pec_codigo = objDAL.incluialtera(Me.pec_codigo, Me.pro_codigo, Me.clf_codigo, Me.fab_codigo,
                                                    Me.sta_codigo, Me.cat_codigo, Me.pec_codbarra, Me.pec_qtdemin,
                                                    Me.pec_qtdemax, Me.pec_qtde, Me.pec_ipi, Me.pec_subtributaria,
                                                    Me.pec_original, Me.pec_valorunitario, Me.pec_mcontrib, Me.pec_margem,
                                                    Me.pec_abreviacao, Me.pec_codigofabricante, Me.pro_descricao,
                                                    Me.pro_precovenda, Me.pro_precocusto, Me.pro_desconto, usuario)
                Else : Throw New Exception("Peça já cadastrada.")
                End If
            Else
                Throw New Exception("Preencha todos os campos.")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return True

    End Function

#End Region

#Region "Get codigo Produto"
    Public Function getproduto(pec_codigo As Integer) As Integer
        Try
            objDAL = New PecaDAL
            Return objDAL.getproduto(pec_codigo)
        Catch ex As Exception
            Throw ex
        End Try
    End Function
#End Region

#Region "Localizar"
    Public Function localizar(descricao As String, tipo As Integer) As DataTable
        Try
            objDAL = New PecaDAL
            objDAL.OrdCodigo = Me.OrdCodigo
            objDAL.VenCodigo = Me.VenCodigo
            Return objDAL.buscar(descricao, tipo)
        Catch ex As Exception
            Throw ex
        End Try
    End Function
#End Region

#Region "Localizar"
    Public Function localizarpecaservico(descricao As String, tipo As Integer) As DataTable
        Try
            objDAL = New PecaDAL
            objDAL.OrdCodigo = Me.OrdCodigo
            Return objDAL.buscarpecaservico(descricao, tipo)
        Catch ex As Exception
            Throw ex
        End Try
    End Function
#End Region

#Region "Validar Réplica de Categoria"
    Function validareplica() As DataTable
        Dim ret As DataTable
        Try

            ret = objfuncoes.ValidaReplica(Me.pro_descricao, Me.pec_codigo, "vw_EstPeca", "pec_codigo", "pro_descricao")
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
#End Region

#Region "Valida Campos obrigatórios"
    Public Function valida() As Boolean
        If (Me.pec_codbarra = Nothing Or Me.pec_codbarra <= 0) Then Throw New Exception("Preencha o código de barras.")
        If Me.sta_codigo = Nothing Or Me.sta_codigo <= 0 Then Throw New Exception("Preencha o campo status.")
        If (Me.pro_descricao = Nothing Or Me.pro_descricao.Trim = "") Then Throw New Exception("Preencha a descrição.")
        If (Me.pec_abreviacao = Nothing Or Me.pec_abreviacao.Trim = "") Then Throw New Exception("Preencha a abreviação.")
        If Me.fab_codigo = Nothing Or Me.fab_codigo <= 0 Then Throw New Exception("Busque um fabricante.")
        If Me.pec_qtdemin = Nothing Or Me.pec_qtdemin <= 0 Then Throw New Exception("Preencha o estoque mínimo.")
        If Me.pec_qtdemax = Nothing Or Me.pec_qtdemax <= 0 Then Throw New Exception("Preencha o estoque máximo.")
        If Me.cat_codigo = Nothing Or Me.cat_codigo <= 0 Then Throw New Exception("Selecione uma categoria.")
        If Me.clf_codigo = Nothing Or Me.clf_codigo <= 0 Then Throw New Exception("Selecione uma clas. fiscal.")
        If Me.pec_codigofabricante = Nothing Or Me.pec_codigofabricante.Trim = "" Then Throw New Exception("Preencha o código original do fabricante.")
        If Me.pec_original = Nothing Or Me.pec_original.Trim = "" Then Throw New Exception("Preencha o código original da peça.")
        If (Me.pec_qtdemax <= Me.pec_qtdemin) Then Throw New Exception("Qtde máxima não pode ser menor que qtde mínima.")
        '  If Me.pro_desconto = Nothing Or Me.pro_desconto <= 0 Then Throw New Exception("Preencha o desconto.")
        Return True
    End Function
#End Region

#Region "Entrada Manual"
    Public Function entradamanual(operacao As String, qtde As Integer, venda As Decimal, custo As Decimal, mc As Decimal, m As Decimal, usuario As Integer) As Boolean
        Dim ret As Boolean = False
        Try
            objDAL = New PecaDAL
            If (validaentradamanual(operacao)) Then
                If (operacao = "Correção Custo") Then
                    qtde = Me.pec_qtde
                Else
                    custo = Me.pro_precocusto
                    venda = Me.pro_precovenda
                    mc = Me.pec_mcontrib
                    m = Me.pec_margem
                End If

                ret = objDAL.entradamanual(pro_codigo, operacao, qtde, custo, venda, mc, m, usuario)
            End If

        Catch ex As Exception
            Throw ex
        End Try
        Return ret
    End Function
#End Region

#Region "Valida Entrada Manual"
    Private Function validaentradamanual(operacao As String) As Boolean
        If (Me.pro_codigo <= 0) Then Throw New Exception("Favor escolher produto")
        If (IsNothing(operacao) Or "" = operacao.Trim) Then Throw New Exception("Selecione uma operação")
        Return True
    End Function
#End Region

#Region "Carregar por codigo do produto"
    Function carregaproduto(codigo As Integer) As PECABLL
        Dim tab As DataTable
        Try
            objDAL = New PecaDAL
            tab = objDAL.buscar(codigo, 8)
            If (tab.Rows.Count > 0) Then
                Me.pec_codigo = Val(tab.Rows(0)("pec_codigo").ToString)
                Me.pro_codigo = Val(tab.Rows(0)("pro_codigo").ToString)
                Me.clf_codigo = Val(tab.Rows(0)("clf_codigo").ToString)
                Me.fab_codigo = Val(tab.Rows(0)("fab_codigo").ToString)
                Me.sta_codigo = Val(tab.Rows(0)("sta_codigo").ToString)
                Me.cat_codigo = Val(tab.Rows(0)("cat_codigo").ToString)
                Me.pec_codbarra = Val(tab.Rows(0)("pec_codbarra").ToString)
                Me.pec_qtdemin = Val(tab.Rows(0)("pec_qtdemin").ToString)
                Me.pec_qtdemax = Val(tab.Rows(0)("pec_qtdemax").ToString)
                Me.pec_qtde = Val(tab.Rows(0)("pec_qtde").ToString)
                If (Not tab.Rows(0)("pec_ipi").ToString = "") Then Me.pec_ipi = Decimal.Parse(tab.Rows(0)("pec_ipi").ToString)
                If (Not tab.Rows(0)("pec_subtributaria").ToString = "") Then Me.pec_subtributaria = Decimal.Parse(tab.Rows(0)("pec_subtributaria").ToString)
                Me.pec_original = tab.Rows(0)("pec_original").ToString
                If (Not tab.Rows(0)("pec_valunitario").ToString = "") Then Me.pec_valorunitario = Decimal.Parse(tab.Rows(0)("pec_valunitario").ToString)
                If (Not tab.Rows(0)("pec_mcontrib").ToString = "") Then Me.pec_mcontrib = Decimal.Parse(tab.Rows(0)("pec_mcontrib").ToString)
                If (Not tab.Rows(0)("pec_margem").ToString = "") Then Me.pec_margem = Decimal.Parse(tab.Rows(0)("pec_margem").ToString)
                Me.pec_abreviacao = tab.Rows(0)("pec_abreviacao").ToString
                Me.pec_codigofabricante = tab.Rows(0)("pec_codigofabricante").ToString
                Me.pro_descricao = tab.Rows(0)("pro_descricao").ToString
                If (Not tab.Rows(0)("pro_precovenda").ToString = "") Then Me.pro_precovenda = Decimal.Parse(tab.Rows(0)("pro_precovenda").ToString)
                If (Not tab.Rows(0)("pro_precocusto").ToString = "") Then Me.pro_precocusto = Decimal.Parse(tab.Rows(0)("pro_precocusto").ToString)
                If (Not tab.Rows(0)("pro_desconto").ToString = "") Then Me.pro_desconto = Decimal.Parse(tab.Rows(0)("pro_desconto").ToString)
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return Me
    End Function
#End Region

#End Region

#Region "Procedimentos"
#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New PecaDAL
            tab = objDAL.buscar(codigo, 1)
            If (tab.Rows.Count > 0) Then
                Me.pec_codigo = Val(tab.Rows(0)("pec_codigo").ToString)
                Me.pro_codigo = Val(tab.Rows(0)("pro_codigo").ToString)
                Me.clf_codigo = Val(tab.Rows(0)("clf_codigo").ToString)
                Me.fab_codigo = Val(tab.Rows(0)("fab_codigo").ToString)
                Me.sta_codigo = Val(tab.Rows(0)("sta_codigo").ToString)
                Me.cat_codigo = Val(tab.Rows(0)("cat_codigo").ToString)
                Me.pec_codbarra = Val(tab.Rows(0)("pec_codbarra").ToString)
                Me.pec_qtdemin = Val(tab.Rows(0)("pec_qtdemin").ToString)
                Me.pec_qtdemax = Val(tab.Rows(0)("pec_qtdemax").ToString)
                Me.pec_qtde = Val(tab.Rows(0)("pec_qtde").ToString)
                If (Not tab.Rows(0)("pec_ipi").ToString = "") Then Me.pec_ipi = Decimal.Parse(tab.Rows(0)("pec_ipi").ToString)
                If (Not tab.Rows(0)("pec_subtributaria").ToString = "") Then Me.pec_subtributaria = Decimal.Parse(tab.Rows(0)("pec_subtributaria").ToString)
                Me.pec_original = tab.Rows(0)("pec_original").ToString
                If (Not tab.Rows(0)("pec_valunitario").ToString = "") Then Me.pec_valorunitario = Decimal.Parse(tab.Rows(0)("pec_valunitario").ToString)
                If (Not tab.Rows(0)("pec_mcontrib").ToString = "") Then Me.pec_mcontrib = Decimal.Parse(tab.Rows(0)("pec_mcontrib").ToString)
                If (Not tab.Rows(0)("pec_margem").ToString = "") Then Me.pec_margem = Decimal.Parse(tab.Rows(0)("pec_margem").ToString)
                Me.pec_abreviacao = tab.Rows(0)("pec_abreviacao").ToString
                Me.pec_codigofabricante = tab.Rows(0)("pec_codigofabricante").ToString
                Me.pro_descricao = tab.Rows(0)("pro_descricao").ToString
                If (Not tab.Rows(0)("pro_precovenda").ToString = "") Then Me.pro_precovenda = Decimal.Parse(tab.Rows(0)("pro_precovenda").ToString)
                If (Not tab.Rows(0)("pro_precocusto").ToString = "") Then Me.pro_precocusto = Decimal.Parse(tab.Rows(0)("pro_precocusto").ToString)
                If (Not tab.Rows(0)("pro_desconto").ToString = "") Then Me.pro_desconto = Decimal.Parse(tab.Rows(0)("pro_desconto").ToString)
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
    End Sub
#End Region

#Region "Limpa Propriedades"
    Sub limpar()
        Me.pec_codigo = Nothing
        Me.pro_codigo = Nothing
        Me.clf_codigo = Nothing
        Me.fab_codigo = Nothing
        Me.sta_codigo = Nothing
        Me.cat_codigo = Nothing
        Me.pec_codbarra = Nothing
        Me.pec_qtdemin = Nothing
        Me.pec_qtdemax = Nothing
        Me.pec_qtde = Nothing
        Me.pec_ipi = Nothing
        Me.pec_subtributaria = Nothing
        Me.pec_original = Nothing
        Me.pec_valorunitario = Nothing
        Me.pec_mcontrib = Nothing
        Me.pec_margem = Nothing
        Me.pec_abreviacao = Nothing
        Me.pec_codigofabricante = Nothing
        Me.pro_descricao = Nothing
        Me.pro_precovenda = Nothing
        Me.pro_precocusto = Nothing
        Me.pro_desconto = Nothing
    End Sub
#End Region
#End Region



End Class
